from utils.get_keyword import GetKeyword
from utils.request_method import SendMethod

class MemberInterface:

    def __init__(self):
        self.url='http://47.108.206.100:8085'

    #发送请求获取响应
    def send_auth_code(self,telephone):
        method='get'
        url=self.url + '/sso/getAuthCode'
        query_params={
            "telephone":telephone
        }
        return SendMethod.send_method(method,url,params=query_params)


    #获取验证码
    def get_authcode(self,telephone):
        response=self.send_auth_code(telephone)
        return GetKeyword.get_keyword(response,'data')


    #获取登录令牌
    def get_token_head(self,body):
        response = self.user_login(body)
        token = GetKeyword.get_keyword(response,'token')
        return {'Authorization': f'Bearer {token}'}



    #会员注册
    def user_register(self,body):
        method='post'
        url=self.url +'/sso/register'
        return SendMethod.send_method(method,url,data=body)

    #会员登录
    def user_login(self,body):
        method='post'
        url=self.url +'/sso/login'
        return SendMethod.send_method(method,url,data=body)



    #获取会员信息
    def get_userinfo(self,body):
        method='get'
        url=self.url +'/sso/info'
        params = {'name': 'tom'}
        headers=self.get_token_head(body)
        return SendMethod.send_method(method=method,url=url,params=params,headers=headers)


    #会员修改密码
    def alter_pwd(self,body,alter_body):
        method='post'
        url=self.url +'/sso/updatePassword'
        headers =self.get_token_head(body)
        return SendMethod.send_method(method,url,data=alter_body,headers=headers)









if __name__ == '__main__':
    member=MemberInterface()
    # print(member.send_auth_code('138123445678'))
    # print(member.get_authcode('13512345678'))
    telephone='13597301389'
    body={
        "authCode":member.get_authcode(telephone),
        'password':'123456',
        'telephone':telephone,
        'username':'海绵宝宝'
    }
    print(member.user_register(body))
    body2 = {
        'username':'海绵宝宝',
        'password': '123456'
    }
    body4 = {
        'username': 'tom',
        'password': '123456'
    }
    # print(member.user_login(body2))

    # print(member.get_token_head(body2))

    # 获取用户信息
    # print(member.get_userinfo(body2))
    body3={
        "authCode":member.get_authcode(telephone),
        'password':'123456',
        'telephone':telephone
    }
    #修改密码
    # print(member.alter_pwd(body2,body3))





